/* store */
import {
  useBaseSelectUserStoreCurrentTabType,
  useBaseSelectUserStoreFilterForm,
  useBaseSelectUserStoreFilterNeedUpdateMap,
} from '@src/component/common/BaseSelectUser/store'
/* util */
import { getFilterFormNotEmptyCount } from '@src/component/common/BaseSelectUser/util'
import { isNotEmpty } from '@src/util/type'
/* vue */
import { computed } from 'vue'
/* types */
import { 
  BaseSelectUserFilterDepartmentForm, 
  BaseSelectUserFilterRoleForm, 
  BaseSelectUserFilterServiceProviderForm 
} from '@src/component/common/BaseSelectUser/types'

function useBaseSelectUserFilter() {
  
  const { isFilterDepartmentFormNotEmpty, isFilterRoleFormNotEmpty, isFilterServiceProviderFormNotEmpty } = useBaseSelectUserStoreFilterForm()
  const { 
    filterNeedUpdateMapDepartment, 
    filterNeedUpdateMapRole, 
    filterNeedUpdateMapServiceProvider 
  } = useBaseSelectUserStoreFilterNeedUpdateMap()
  
  const isFetchDepartmentRootList = computed(() => {
    return isFilterDepartmentFormNotEmpty.value || filterNeedUpdateMapDepartment.value
  })
  
  const isFetchRoleList = computed(() => {
    return isFilterRoleFormNotEmpty.value || filterNeedUpdateMapRole.value
  })
  
  const isFetchServiceProviderList = computed(() => {
    return isFilterServiceProviderFormNotEmpty.value || filterNeedUpdateMapServiceProvider.value
  })
  
  return {
    isFetchDepartmentRootList,
    isFetchRoleList,
    isFetchServiceProviderList
  }
}

/** 
 * @description 筛选表单
*/
function useBaseSelectUserFilterForm() {
  
  const { setFilterDepartmentForm, setFilterRoleForm, setFilterServiceProviderForm } = useBaseSelectUserStoreFilterForm()
  
  const initFilterDepartmentForm = () => {
    
    const filterDepartmentForm: BaseSelectUserFilterDepartmentForm = {
      stateList: [],
      labelIdList: [],
      tagName: '',
      qualificationManagement:[]
    }
    
    setFilterDepartmentForm(filterDepartmentForm)
  }
  
  const initFilterRoleForm = () => {
      
    const filterRoleForm: BaseSelectUserFilterRoleForm = {
      stateList: [],
      labelIdList: [],
      roleName: '',
      qualificationManagement:[]
    }
    
    setFilterRoleForm(filterRoleForm)
  }
  
  const initFilterServiceProviderForm = () => {
      
    const filterServiceProviderForm: BaseSelectUserFilterServiceProviderForm = {
      stateList: [],
      spList: [],
      spTypeList: [],
      spName: '',
      qualificationManagement:[]
    }
    
    setFilterServiceProviderForm(filterServiceProviderForm)
  }
  
  return {
    initFilterDepartmentForm,
    initFilterRoleForm,
    initFilterServiceProviderForm
  }
}

/** 
 * @description 筛选表单 筛选项数量
*/
function useBaseSelectUserFilterCount() {
  
  const { filterDepartmentForm, filterRoleForm, filterServiceProviderForm } = useBaseSelectUserStoreFilterForm()
  const { isDepartmentTab, isRoleTab, isServiceProviderTab } = useBaseSelectUserStoreCurrentTabType()
  
  const filterDepartmentFormCount = computed(() => {
    return getFilterFormNotEmptyCount(filterDepartmentForm.value)
  })
  
  const filterRoleFormCount = computed(() => {
    return getFilterFormNotEmptyCount(filterRoleForm.value)
  })
  
  const filterServiceProviderFormCount = computed(() => {
    return getFilterFormNotEmptyCount(filterServiceProviderForm.value)
  })
  
  const currentFilterFormCount = computed(() => {
    
    if (isDepartmentTab.value) {
      return filterDepartmentFormCount.value
    }
    
    if (isRoleTab.value) {
      return filterRoleFormCount.value
    }
    
    if (isServiceProviderTab.value) {
      return filterServiceProviderFormCount.value
    }
    
    return 0
    
  })
  
  const filterDepartmentFormCountNotEmpty = computed(() => {
    return filterDepartmentFormCount.value > 0
  })
  
  const filterRoleFormCountNotEmpty = computed(() => {
    return filterRoleFormCount.value > 0
  })
  
  const filterServiceProviderFormCountNotEmpty = computed(() => {
    return filterServiceProviderFormCount.value > 0
  })
  
  return {
    currentFilterFormCount,
    filterDepartmentFormCount,
    filterRoleFormCount,
    filterServiceProviderFormCount,
    filterDepartmentFormCountNotEmpty,
    filterRoleFormCountNotEmpty,
    filterServiceProviderFormCountNotEmpty
  }
}

export {
  useBaseSelectUserFilter,
  useBaseSelectUserFilterForm,
  useBaseSelectUserFilterCount
}
